package util

import (
	"bytes"
	"dag2.0/gat1400-proxy/util/times"
	"math/rand"
	"strconv"
	"time"
)

func GenerateDataRecordId(deviceID, subTypeCode, timeStr, dateType string) string {
	var buff bytes.Buffer
	if len(deviceID) > 20 {
		deviceID = deviceID[:20]
	}
	buff.WriteString(deviceID)           //20位设备国标ID
	buff.WriteString(subTypeCode)        //02标识数据种类时图像
	buff.WriteString(timeStr)            //生成时间
	buff.WriteString(RandomNumberStr(5)) //5位随机序号
	buff.WriteString(dateType)           //数据类型
	buff.WriteString(RandomNumberStr(5)) //5位随机序号
	return buff.String()
}

//生成固定长度的随机数字字符串
func RandomNumberStr(len int) string {
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	var result bytes.Buffer
	for i := 0; i < len; i++ {
		result.WriteString(strconv.Itoa(r.Intn(10)))
	}
	return result.String()
}
//生成订阅ID
func GenerateSubscribeID() string {
	var buff bytes.Buffer
	buff.WriteString("010000000000")
	buff.WriteString("03")
	buff.WriteString(times.Time2StrWithFormat(times.DateFormatNoSpan))
	buff.WriteString(RandomNumberStr(5))
	return buff.String()
}
func GenerateSubscribeNotificationID()  string{
	var buff bytes.Buffer
	buff.WriteString("010000000000")
	buff.WriteString("04")
	buff.WriteString(times.Time2StrWithFormat(times.DateFormatNoSpan))
	buff.WriteString(RandomNumberStr(5))
	return buff.String()
}